﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ProJunGongRule
    {
        #region 竣工 

        #region 导出
        /// <summary>
        /// 用户列表（导出Excel）
        /// </summary>
        /// <returns></returns>
        public static async Task<MemoryStream> GetExportJunGongList(List<string> ids, List<ColumnModel> cols)
        {
            //取出数据源
            DataTable exportTable = await GetExportJunGongList(ids);
            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = "竣工管理列表";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = "竣工管理列表.xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }

        public async static Task<DataTable> GetExportJunGongList(List<string> ids)
        {
            //throw new NotImplementedException();
            string yids = string.Join("','", ids);
            yids = "'" + yids + "'";
            string strsql = "select * from View_ProJunGongLists where tid in (" + yids + ")";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #endregion


        //查询竣工列表
        public static async Task<(IEnumerable<dynamic>, int)> GetJunGongList(ProJunGongQuerry querry, string strdepids)//uid
        {
            string str = "";

            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmfzr)))
            {
                str = str + " and xmfzr like '%" + querry.xmfzr + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Jf)))
            {
                str = str + " and jf like ''%" + querry.Jf + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Yf)))
            {
                str = str + " and yf like ''%" + querry.Yf + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Htid)))
            {
                str = str + " and htid like ''%" + querry.Htid + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.JgStatus)))
            {
                str = str + " and jgzt = ''" + querry.JgStatus + "''";
            }

            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = ''" + querry.status + "''";
            }

            //必须是审核过的合同
            str += " and htid in (select id from CProContract where status = '2')";

            str += " and xmid not in (select xmid from CProGD)";

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProJunGongLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            //bool isAsc = true;
            //if (querry.sort != "Asc" && querry.sort != "asc")
            //{
            //    isAsc = false;
            //}

            bool isAsc = querry.sort.ToLower() == "asc";

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }
        //根据htid获取竣工
        public static async Task<IEnumerable<dynamic>> GetJGByhtid(string htid)
        {
            string sql = "select * from View_ProJunGongLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }

        public static async Task<dynamic> GetJGByid(string id)
        {
            string sql = "select * from View_ProJunGongLists where id = '" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        //新增修改竣工
        public static async Task InsertCProJG(CProJG jg, string strlx, string uid)
        {
            if (strlx == "add")
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //先删除
                    string strsql = "delete from CProJG where htid = '" + jg.htid + "'";
                    await irp.ExecuteSql(strsql);

                    //再新增
                    await irp.Insert(jg);

                    return true;

                });
            }
            else
            {
                await new RepositoryFactory().BaseRepository().Update(jg);
            }

        }

        //删除多条竣工-根据htid
        public static async Task<bool> DeleteCProJGMulti(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
                            {
                                foreach (BaseDeleteMulti idm in ids.ids)
                                {
                                    string delsql = "delete from CProJG where id = '" + idm.id + "'";

                                    await irp.ExecuteSql(delsql);
                                }

                                return true;
                            });
        }

        #endregion

        #region 结算

        #region 导出
        /// <summary>
        /// 用户列表（导出Excel）
        /// </summary>
        /// <returns></returns>
        public static async Task<MemoryStream> GetExportJieSuanList(List<string> ids, List<ColumnModel> cols)
        {
            //取出数据源
            DataTable exportTable = await GetExportJunGongList(ids);
            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = "结算管理列表";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = "结算管理列表.xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }

        public async static Task<DataTable> GetExportJieSuanList(List<string> ids)
        {
            //throw new NotImplementedException();
            string yids = string.Join("','", ids);
            yids = "'" + yids + "'";
            string strsql = "select * from View_ProJSLists where tid in (" + yids + ")";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #endregion

        //查询结算列表
        public static async Task<(IEnumerable<dynamic>, int)> GetJieSuanList(ProJieSuanQuerry querry, string strdepids)//string uid
        {
            string str = "";

            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and buildunit like '%" + querry.jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Jf)))
            {
                str = str + " and jf like '%" + querry.Jf + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Htid)))
            {
                str = str + " and htid like ''%" + querry.Htid + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.htbh)))
            {
                str = str + " and htbh like ''%" + querry.htbh + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and spstatus = ''" + querry.Status + "'";
            }

            if (!(string.IsNullOrEmpty(querry.jszt)))
            {
                str = str + " and jszt = ''" + querry.jszt + "'";
            }

            if (!(string.IsNullOrEmpty(querry.xmfzr)))
            {
                str = str + " and ProjectManager like ''%" + querry.xmfzr + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmid = ''" + querry.xmid + "'";
            }
            if (!(string.IsNullOrEmpty(querry.jszt)))
            {
                str = str + " and jszt = ''" + querry.jszt + "'";
            }

            //必须是审核过的合同
            str += " and htid in (select id from CProContract where status = '2')";

            str += " and xmid not in (select xmid from CProGD)";

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProJSLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");

            bool isAsc = querry.sort.ToLower() == "asc";

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
                //querry.column = " (case when ISNULL(status,-2) = 2 then 20 else ISNULL(status,-2) end) asc,id ";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //根据htid获取结算
        public static async Task<dynamic> GetJSByhtid(string htid)
        {
            string sql = "select * from View_ProJSLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        //根据id获取结算
        public static async Task<dynamic> GetJSByid(string id)
        {
            string sql = "select * from View_ProJSLists where id = '" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<CProJS> GetJSTableByid(string id)
        {
            string sql = "select * from CProJS where id = '" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity<CProJS>(sql, null);
        }

        public static async Task<IEnumerable<cprojsfile>> GetJSfileTableByhtid(string htid)
        {
            string sql = "select * from cprojsfile where jsid = (select id from cprojs where htid = '"+ htid +"')";
            return await new RepositoryFactory().BaseRepository().FindList<cprojsfile>(sql, null);
        }

        public static async Task<IEnumerable<cprojsfile>> GetJSfileTableByid(string id)
        {
            string sql = "select * from cprojsfile where jsid = '" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindList<cprojsfile>(sql, null);
        }

        public async static Task<List<string>> GetXMJSID(string htid)
        {
            string strsql = "exec getprojectjsids '" + htid + "'";
            DataTable dts = await new RepositoryFactory().BaseRepository().FindTable(strsql);
            List<string> list = new List<string>();
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                list.Add(dts.Rows[i]["id"].ToString());
            }

            return list;
        }

        //新增修改项目送审
        public static async Task InsertCProJSSS(CProJS js, List<cprojsfileJson> fileinfos, string strlx, string uid)
        {
            if (strlx == "add")
            {

                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //先删除
                    string strsql = "delete from CProJS where htid = '" + js.htid + "'";
                    await irp.ExecuteSql(strsql);

                    //删除附件
                    strsql = "delete from cprojsfile where jsid = '" + js.id + "'";
                    await irp.ExecuteSql(strsql);

                    for (int i = 0; i < fileinfos.Count; i++)
                    {
                        cprojsfile fileinfo = new cprojsfile();
                        fileinfo.id = CommonHelper.GetUniqueId().ToString();
                        fileinfo.jsid = js.id;
                        fileinfo.indexs = (i + 1);

                        fileinfo.formCode = fileinfos[i].formCode;
                        fileinfo.file = fileinfos[i].file?.ToJson() ?? "";
                        fileinfo.filenum = fileinfos[i].filenum;
                        fileinfo.options = fileinfos[i].options?.ToJson() ?? "";
                        fileinfo.title = fileinfos[i].title; 
                        await irp.Insert(fileinfo);
                    }

                    //再新增
                    await irp.Insert(js);

                    return true;
                });

                //await new RepositoryFactory().BaseRepository().Insert(js);
            }
            else
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //await new RepositoryFactory().BaseRepository().Update(js);

                    //删除附件
                    string strsql = "delete from cprojsfile where jsid = '" + js.id + "'";
                    await irp.ExecuteSql(strsql);

                    for (int i = 0; i < fileinfos.Count; i++)
                    {
                        //fileinfos[i].id = CommonHelper.GetUniqueId().ToString();
                        //fileinfos[i].jsid = js.id;
                        //fileinfos[i].indexs = (i + 1);

                        //await irp.Insert(fileinfos[i]);

                        cprojsfile fileinfo = new cprojsfile();
                        fileinfo.id = CommonHelper.GetUniqueId().ToString();
                        fileinfo.jsid = js.id;
                        fileinfo.indexs = (i + 1);


                        fileinfo.formCode = fileinfos[i].formCode;
                        fileinfo.file = fileinfos[i].file?.ToJson() ?? "";
                        fileinfo.filenum = fileinfos[i].filenum;
                        fileinfo.options = fileinfos[i].options?.ToJson() ?? "";
                        fileinfo.title = fileinfos[i].title;

                        await irp.Insert(fileinfo);
                    }

                    await irp.Update(js);

                    return true;
                });
            }


        }
        //新增修改项目审计
        public static async Task InsertCProJSSJ(CProJS js, string uid)
        {

            await new RepositoryFactory().BaseRepository().Update(js);

        }
        public static async Task<bool> GetXMISJUSAsync(string htid)
        {
            string sql = "select * from CProJuS where htid in (select xmid from dbo.CProManageLX where id in\r\n (select dxid from dbo.CProContract where htid = '" + htid + "')) and status = '2'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            bool flag = false;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                flag = true;
            }
            return flag;
        }

        //新增修改不审计
        public static async Task InsertCProJSBSJ(List<CProJS> list, string uid)
        {
            await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //先删除
                    string strsql = "delete from CProJS where htid = '" + list[i].htid + "'";
                    await irp.ExecuteSql(strsql);

                    //再新增
                    await irp.Insert(list[i]);
                }


                return true;

            });

            //CProJS count = await new RepositoryFactory().BaseRepository().FindEntity<CProJS>(new { htid = js.htid });
            //if (count is null)
            //{
            //    //string strid = CommonHelper.GetUniqueId().ToString();
            //    //js.id = strid;
            //    await new RepositoryFactory().BaseRepository().Insert(js);
            //}
            //else
            //{
            //    //if (!await BaseEditPower.GetEditPower(count.id, BaseEditPower.TableName.结算))
            //    //{
            //    //    throw new Exception("请确认数据权限");
            //    //}

            //    await new RepositoryFactory().BaseRepository().Update(js);
            //}
        }

        //新增修改不审计
        public static async Task InsertCProJSBSJ(CProJS js, string uid)
        {
            CProJS count = await new RepositoryFactory().BaseRepository().FindEntity<CProJS>(new { htid = js.htid });
            if (count is null)
            {
                //string strid = CommonHelper.GetUniqueId().ToString();
                //js.id = strid;
                await new RepositoryFactory().BaseRepository().Insert(js);
            }
            else
            {
                //if (!await BaseEditPower.GetEditPower(count.id, BaseEditPower.TableName.结算))
                //{
                //    throw new Exception("请确认数据权限");
                //}

                await new RepositoryFactory().BaseRepository().Update(js);
            }
        }

        //需要送审
        public static async Task DeleteCProJSMulti2(List<string> htids)
        {
            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (string idm in htids)
                {
                    string delsql = "delete from CProJS where htid = '" + idm + "'";

                    await iRep.ExecuteSql(delsql);
                }
                return true;
            });
        }

        //删除多条结算-根据htid
        public static async Task DeleteCProJSMulti(Root ids)
        {
            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    string delsql = "delete from CProJS where id = '" + idm.id + "'";

                    await iRep.ExecuteSql(delsql);
                }
                return true;
            });
        }


        #endregion

        #region 现场管理
        //public static async Task<(IEnumerable<dynamic>, int)> GetHTLogsIndexList(dynamic dye, string strdepids)//uid
        //{

        //    string xmmc = dye.xmmc;
        //    string jsdw = dye.jsdw;
        //    string xmnf = dye.xmnf;


        //    string current = dye.current;
        //    string size = dye.size;
        //    string sort = dye.sort ?? "";
        //    string column = dye.column ?? "";


        //    string str = "";

        //    if (!(string.IsNullOrEmpty(xmmc)))
        //    {
        //        str = str + " and xmmc like '%" + xmmc + "%'";
        //    }

        //    if (!(string.IsNullOrEmpty(jsdw)))
        //    {
        //        str = str + " and jf like ''%" + jsdw + "%'";
        //    }

        //    if (!(string.IsNullOrEmpty(xmnf)))
        //    {
        //        str = str + " and xmnf = ''" + xmnf + "''";
        //    }

        //    //await UserRule.GetUserDepid(uid)
        //    string sql = "select * from View_ProYiJiaoLists where departid in " + strdepids + str.Replace("''", "'");

        //    bool isAsc = sort.ToLower() == "asc";

        //    if (string.IsNullOrEmpty(column))
        //    {
        //        column = "id";
        //    }
        //    return await new RepositoryFactory().BaseRepository().FindList(sql, null, column, isAsc, size.ToInt(), current.ToInt());

        //}

        public static async Task<(IEnumerable<dynamic>, int)> GetXMLogsIndexList(CProManageQQQuerry querry, string strdepids)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.Jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmfl)))
            {
                str = str + " and xmflname = '" + querry.Xmfl + "'";
            }
            if (!(string.IsNullOrEmpty(querry.year)))
            {
                str = str + " and xmnf = '" + querry.year + "'";
            }
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = '" + querry.status + "'";
            }

            if (!(string.IsNullOrEmpty(querry.sftb)))
            {
                str = str + " and qqzt = '" + querry.sftb + "'";
            }

            str += " and id not in (select xmid from CProGD)";

            string strsql = "select * from View_ProQQXCLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(strsql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public async static Task<(IEnumerable<dynamic>, int)> GetLogsIndexList(dynamic dye, string lx, string strdepids)
        {

            //dye.xmmc = xmmc;
            //dye.reporter = reporter;//汇报人
            //dye.yearRange = yearRange;

            string xmmc = dye.xmmc;
            string reporter = dye.reporter;
            //string status = dye.status;
            string yearRange = dye.yearRange;
            string rtype = dye.rtype;
            string jsdw = dye.jsdw;
            string zgzt = dye.zgzt;

            string current = dye.current;

            string size = dye.size;
            string sort = dye.sort ?? "";
            string column = dye.column ?? "";

            string str = "";
            if (!(string.IsNullOrEmpty(xmmc)))
            {
                str = str + " and xmmc like '%" + xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(jsdw)))
            {
                str = str + " and buildunit like '%"+ jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(reporter)))
            {
                str = str + " and tbr = '" + reporter + "'";
            }
            if (!(string.IsNullOrEmpty(lx)))
            {
                str = str + " and lx = '" + lx + "'";
            }

            if (!(string.IsNullOrEmpty(rtype)))
            {
                str = str + " and rtype = '" + rtype + "'";
            }
            //整改完成
            if (!(string.IsNullOrEmpty(zgzt)))
            {
                str = str + " and zgzt = '" + zgzt + "'";
            }

            if (!(string.IsNullOrEmpty(yearRange)))
            {
                string[] strs = yearRange.Split('_');

                str = str + " and tbrq >= '" + strs[0] + "'";

                str = str + " and tbrq <= '" + strs[1] + "'";
            }



            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProHTLogLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");

            bool isAsc = sort.ToLower() == "asc";

            if (string.IsNullOrEmpty(column))
            {
                column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, column, isAsc, size.ToInt(), current.ToInt());

        }

        public async static Task<DataTable> GetHTLogMBList(string status)
        {
            string strsql = "select * from CProConLogMB where status = '2'";
            if (status == "all")
            {
                strsql = "select * from CProConLogMB ";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }


        public async static Task<DataTable> GetHTLogMBByid(string logid)
        {
            string strsql = "select * from CProConLogMB where id = '" + logid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);

        }

        public async static Task<int> InsertHTLogMB(CProConLogMB logmb, string ope)
        {
            if (ope == "add")
            {
                return await new RepositoryFactory().BaseRepository().Insert(logmb);
            }
            else// (ope == "edit")
            {
                return await new RepositoryFactory().BaseRepository().Update(logmb);
            }

        }

        public async static Task<bool> DeleteHTLogMB(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    await irp.DeleteAny<CProConLogMB>(new { id = idm.id });


                }
                return true;
            });

            //return await new RepositoryFactory().BaseRepository().DeleteAny<CProConBG>(new { id = bgid });
        }


        public async static Task<DataTable> GetHTLogListByhtid(string strhtid, string strlx)
        {
            string strsql = "select * from View_ProHTLogLists where htid = '" + strhtid + "'";
            if (!string.IsNullOrEmpty(strlx))
            {
                strsql = "select * from View_ProHTLogLists where htid = '" + strhtid + "' and lx = '" + strlx + "'";
            }

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        public async static Task<DataTable> GetHTLogListByxmid(string strxmid, string strlx, string reptype)
        {
            string strsql = "select * from View_ProHTLogLists where xmid = '" + strxmid + "' order by tbrq desc";
            if (!string.IsNullOrEmpty(strlx))
            {
                strsql = "select * from View_ProHTLogLists where xmid = '" + strxmid + "' and lx = '" + strlx + "' order by tbrq desc";

                if (!string.IsNullOrEmpty(reptype))
                {
                    strsql = "select * from View_ProHTLogLists where xmid = '" + strxmid + "' and lx = '" + strlx + "' and rtype = '" + reptype + "' order by tbrq desc";
                }
            }

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }


        public async static Task<DataTable> GetHTLogByid(string logid)
        {
            string strsql = "select * from View_ProHTLogLists where id = '" + logid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);

        }

        public async static Task<int> InsertHTLog(CProConLog log, string ope)
        {
            if (ope == "add")
            {
                //log.status = BaseEditPower.GetSPEnumValue("待提交");

                return await new RepositoryFactory().BaseRepository().Insert(log);
            }
            else// (ope == "edit")
            {
                return await new RepositoryFactory().BaseRepository().Update(log);
            }

        }

        public async static Task<bool> DeleteHTLog(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    await irp.DeleteAny<CProConLog>(new { id = idm.id });


                }
                return true;
            });

            //return await new RepositoryFactory().BaseRepository().DeleteAny<CProConBG>(new { id = bgid });
        }

        #endregion

        #region 已交
        //查询竣工列表
        public static async Task<(IEnumerable<dynamic>, int)> GetyijiaoList(ProYiJiaoQuerry querry, string strdepids)//uid
        {
            string str = "";

            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmfzr)))
            {
                str = str + " and xmfzr like '%" + querry.xmfzr + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Jf)))
            {
                str = str + " and jf like ''%" + querry.Jf + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Yf)))
            {
                str = str + " and yf like ''%" + querry.Yf + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Htid)))
            {
                str = str + " and htid like ''%" + querry.Htid + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.YjStatus)))
            {
                str = str + " and yjzt = ''" + querry.YjStatus + "''";
            }

            if (!(string.IsNullOrEmpty(querry.xmnf)))
            {
                str = str + " and xmnf = ''" + querry.xmnf + "''";
            }
            if (!(string.IsNullOrEmpty(querry.xmlx)))
            {
                str = str + " and xmflname = ''" + querry.xmlx + "''";
            }

            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = ''" + querry.status + "''";
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProYiJiaoLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            //bool isAsc = true;
            //if (querry.sort != "Asc" && querry.sort != "asc")
            //{
            //    isAsc = false;
            //}
            bool isAsc = querry.sort.ToLower() == "asc";

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }
        //根据htid获取竣工
        public static async Task<dynamic> GetYJByhtid(string htid)
        {
            string sql = "select * from View_ProYiJiaoLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<dynamic> GetYJByid(string id)
        {
            string sql = "select * from View_ProYiJiaoLists where id = '" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<IEnumerable<dynamic>> GetYJMXSByYjid(string yjid)
        {
            string sql = "select * from CProYiJiaoMX where yjid = '" + yjid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }

        public static async Task<IEnumerable<dynamic>> GetYJMXSByHTid(string htid)
        {
            string sql = "select * from CProYiJiaoMX where yjid in (select id from CProYiJiao where htid = '" + htid + "')";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }

        //新增修改竣工
        public static async Task InsertCProYJ(CProYiJiao yj, List<CProYiJiaoMX> mxs, string strlx, string uid)
        {
            await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                string strsql = "delete from CProYiJiaoMX where yjid = '" + yj.id + "'";
                await irp.ExecuteSql(strsql);
                //先删除
                strsql = "delete from CProYiJiao where htid = '" + yj.htid + "'";
                await irp.ExecuteSql(strsql);

                //再新增
                await irp.Insert(yj);

                for (int i = 0; i < mxs.Count; i++)
                {
                    mxs[i].id = CommonHelper.GetUniqueId().ToString();
                    mxs[i].yjid = yj.id;
                    //再新增
                    await irp.Insert(mxs[i]);
                }

                return true;

            });
        }

        //删除多条竣工-根据htid
        public static async Task<bool> DeleteCProYJMulti(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    string strsql = "delete from CProYiJiaoMX where yjid = '" + idm.id + "'";
                    await irp.ExecuteSql(strsql);

                    string delsql = "delete from CProYiJiao where id = '" + idm.id + "'";
                    await irp.ExecuteSql(delsql);
                }

                return true;
            });
        }


        #endregion

        #region 合同缺陷期


        //查询结算列表
        public static async Task<(IEnumerable<dynamic>, int)> GetHTqxqList(ProHTQXQQuerry querry, string strdepids)//string uid
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jf)))
            {
                str = str + " and jf like '%" + querry.Jf + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like ''%" + querry.dxmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Htid)))
            {
                str = str + " and htid like ''%" + querry.Htid + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.htbh)))
            {
                str = str + " and htbh like ''%" + querry.htbh + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and spstatus = ''" + querry.Status + "'";

                if (querry.Status == "0")
                {
                    str = str + " and isnull(jgrq,'') <> '' and isnull(sdrq,'') <> '' ";
                }
            }

            if (!(string.IsNullOrEmpty(querry.qxqzt)))
            {
                str = str + " and qxqzt = ''" + querry.qxqzt + "'";
            }


            //必须是审核过的合同
            str += " and htid in (select id from CProContract where status = '2')";

            str += " and xmid not in (select xmid from CProGD)";

            string sql = "select * from View_ProHTQXQLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");

            bool isAsc = querry.sort.ToLower() == "asc";

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //删除多条结算-根据htid
        public static async Task DeleteHTQXQMulti(Root ids)
        {
            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    string delsql = "delete from CProConQXQ where id = '" + idm.id + "'";

                    await iRep.ExecuteSql(delsql);
                }
                return true;
            });
        }

        public static async Task InsertHTQXQ(CProConQXQ htqxq, string strope)
        {
            if (strope == "add")
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //先删除
                    string strsql = "delete from CProConQXQ where id = '" + htqxq.id + "'";
                    await irp.ExecuteSql(strsql);

                    //再新增
                    await irp.Insert(htqxq);

                    return true;

                });
            }
            else
            {
                await new RepositoryFactory().BaseRepository().Update(htqxq);
            }
        }

        public async static Task<dynamic> GetHTQXQByhtId(string htid)
        {
            string strsql = "select * from View_ProHTQXQLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity<dynamic>(strsql);
        }

        public async static Task<dynamic> GetHTQXQByqxqId(string qxqid)
        {
            string strsql = "select * from View_ProHTQXQLists where id = '" + qxqid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity<dynamic>(strsql);
        }

        #endregion


        #region 项目进度报表
        public static async Task<List<BDICNODECONFIGURATION>> GetNodeConfig()
        {
            //IallDataBase iallData = new RepositoryFactory().BaseRepository();

            string strsql = "select * from bdicnodeconfiguration order by INDEXS asc";
            //DataTable dts = await iallData.FindTable(strsql);

            DataTable dts = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);

            List<BDICNODECONFIGURATION> listConfig = new List<BDICNODECONFIGURATION>();
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                string strid = dts.Rows[i]["id"].ToString();
                string strjdmc = dts.Rows[i]["jdmc"].ToString();
                string strjdjc = dts.Rows[i]["jdjc"].ToString();
                string indexs = dts.Rows[i]["indexs"].ToString();
                string strproshow = dts.Rows[i]["proshow"].ToString();
                string strtendersshow = dts.Rows[i]["tendersshow"].ToString();

                listConfig.Add(new BDICNODECONFIGURATION() { id = strid, jdmc = strjdmc, jdjc = strjdjc, indexs = CommonHelper.ParseInt(indexs), proshow = strproshow == "1", tendersshow = strtendersshow == "1" });
            }

            listConfig = listConfig.OrderBy(it => it.indexs).ToList();
            return listConfig;

        }
        public static async Task<(IEnumerable<dynamic>, int)> GetXMJDReportList(ProReportQuerry querry, string strdepids)
        {
            //querry.sort = querry.sort ?? new Dictionary<string, string>();
            //if (!querry.sort.Any())
            //{
            //    querry.sort.Add("xmid", "desc");
            //    querry.sort.Add("rid", "asc");
            //}

            //RefAsync<int> total = 0;
            //var queryList = ListRule.QueryableList<dynamic>("select * from view_proreport_jd_lists",
            //    querry,
            //    strdepids,
            //    (sqlquery, rep) =>
            //    {
            //        //id not in (select xmid from CProGD)
            //        sqlquery.Where(" 1=1 ");

            //        sqlquery.Where(" 1=1 ");

            //    });
            //var list = await queryList.ToPageListAsync(querry.current, querry.size, total);


            //return (list, total);


            string str = " where 1=1 ";

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.departid)))
            {
                str = str + " and departid = '" + querry.departid + "'";
            }

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.buildunit)))
            {
                str = str + " and buildunit like '%" + querry.buildunit + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmid = '" + querry.xmid + "'";
            }

            if (!(string.IsNullOrEmpty(querry.id)))
            {
                str = str + " and id = '" + querry.id + "'";
            }


            string sql = "select * from view_proreport_jd_lists "+ str;


            bool isAsc = (querry.sort?.ToLower() ?? "asc") == "asc";

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //查询 标段
        public static async Task<List<jdnodename>> GetXMJD_DX_ReportList(string xmid, string strtype)
        {
            //IallDataBase iallData = new RepositoryFactory().BaseRepository();
            string strsql = "select * from view_proreport_jd_dx_lists where xmid = '" + xmid + "'";
            if (strtype == "tender")
            {
                strsql = "select * from view_proreport_jd_dx_lists where id = '" + xmid + "'";
            }
            var lists = await new RepositoryFactory().BaseRepository().FindList<jdnodename>(strsql);
            return lists.ToList();
        }


        public static async Task<IEnumerable<dynamic>> GetXMJDReportDetailsList(string vname, string xmid, string strwhereid)
        {
            //IallDataBase iallData = new RepositoryFactory().BaseRepository();
            string strsql = "select * from " + vname + " where " + strwhereid + " = '" + xmid + "'";

            var lists = await new RepositoryFactory().BaseRepository().FindList(strsql);
            return lists;
        }
        #endregion
    }
}
